var util = require('../../utils/util.js');
// 云调用初始化
const db = wx.cloud.database()
Page({
  data: {
    option1: [{
        text: '最新文章',
        value: 0
      },
      {
        text: '展示文章',
        value: 1
      },
      {
        text: '科普文章',
        value: 2
      },
    ],
    option2: [{
      text: '默认排序',
      value: 0
    }],
    value1: 0,
    value2: 0,
    //预览
    theme: '暂无内容',
    Image: '',
    type: '类型',
    Boolean: false,
    text1: '',
    text2: '',
    //图片数据
    addimg: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/admin/article/zhaoxiangji.png?sign=88db73d59f833bde187f6ef7aeaa7a4e&t=1641287827',
    blankimg: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/admin/article/kong1.png?sign=dc6cf59ff70e55ea8414926aef941a68&t=1641283515',
    img1: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/admin/article/qianbi.png?sign=fb101f8002210e572a9334972d2f6deb&t=1641289021',
  },
  onLoad: function () {},
  //图片导入函数
  Getimage: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        //console.log(res.tempFiles[0].path)
        that.setData({
          Image: res.tempFiles[0].path,
          Boolean: true,
        })
      }
    })
  },
  //文本函数
  Settheme: function (res) {
    this.setData({
      theme: res.detail.value,
      text1: res.detail.value
    })
  },
  Settype: function (res) {
    this.setData({
      type: res.detail.value,
      text2: res.detail.value
    })
  },
  //提交上传函数
  Confirm: function () {
    let that = this
    var data1 = that.data.text1
    var data2 = that.data.Image
    var data3 = that.data.text2
    if (data1 != '' & data2 != '' & data3 != '') {
      var uptime = util.formatTime(new Date());
      wx.cloud.uploadFile({
        cloudPath: "admin/images/" + uptime + ".jpg",
        filePath: data2, // 文件路径
        success: res => {
          var imglink = res.fileID
          //console.log(res.fileID)
          db.collection('news', ).add({
            data: {
              pic: imglink, //图片链接
              text: data1, //文章介绍
              type_id: 'news01', //类型id
              Time: uptime, //上传时间
              type: data3,
            },
            success: function (res) {
              wx.showLoading({
                title: '上传中',
              })
              setTimeout(function () {
                wx.showToast({
                  title: '上传成功',
                  icon: 'success',
                  duration: 2000
                })
              }, 3000)
            }
          })
        },
        fail: err => {
          console.log('上传失败')
        }
      })
    } else {
      wx.showToast({
        title: '请将内容填写完整',
        icon: 'error',
        duration: 1500
      })
    }
  },
  Preview: function () {
    var that = this
    var imgpath = [that.data.Image]
    wx.previewImage({
      current: '',
      urls: imgpath,
    })
  },
  setclear: function () {
    this.setData({
      Boolean: false,
      theme: '暂无内容',
      type: '类型',
      text1: '',
      text2: '',
    })
  },

  getselect:function(){
    let that=this
    var value=that.data.value1
    console.log(value)
  }
})